package com.itheima.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.Utils.POIUtils;
import com.itheima.common.MessageConst;
import com.itheima.entity.Result;
import com.itheima.exception.BusinessRuntimeException;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSettingService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @ClassName OrderSettingController
 * @Description: TODO
 * @Author 郑乾
 * @Date 2020-06-26
 * @Version V1.0
 **/
@RestController
@RequestMapping("/ordersetting")
@Slf4j
public class OrderSettingController {
    @Reference
    OrderSettingService orderSettingService;

    @RequestMapping("/upload")
    public Result upload(MultipartFile excelFile) {
        log.debug(excelFile.toString());
        try {
            //读取excel中的数据
            List<String[]> strList = POIUtils.readExcel(excelFile);
            //将读取到的list集合数据转换为List<OrderSetting>并封装
            List<OrderSetting> orderSettingList = new ArrayList<>();
            for (String[] strs : strList) {
                OrderSetting orderSetting = new OrderSetting();
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
                Date orderDate = sdf.parse(strs[0]);
                //将字符串转为 date类型 数值 封装进入ordersetting
                orderSetting.setOrderDate(orderDate);
                orderSetting.setNumber(Integer.parseInt(strs[1]));
                //封装进入集合
                orderSettingList.add(orderSetting);
            }
            log.debug(orderSettingList.toString());
            orderSettingService.addOrderSettingList(orderSettingList);
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessRuntimeException(MessageConst.IMPORT_ORDERSETTING_FAIL);
        }
        return new Result(true, MessageConst.IMPORT_ORDERSETTING_SUCCESS);
    }

    @RequestMapping("/findByMonth")
    public Result findByMonth(String date) throws ParseException {
        List<OrderSetting> orderSettingList = orderSettingService.findByMonth(date);
        List<Map<String, Object>> mapList = new ArrayList<>();
        for (OrderSetting orderSetting : orderSettingList) {
            Map<String, Object> map = new HashMap<>();
            Date orderDate = orderSetting.getOrderDate();
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd");
            String format = simpleDateFormat.format(orderDate);
            map.put("date", format);
            map.put("number", orderSetting.getNumber());
            map.put("reservations", orderSetting.getReservations());
            mapList.add(map);
        }
        return new Result(true, MessageConst.GET_ORDERSETTING_SUCCESS, mapList);
    }

    @RequestMapping("saveOrEdit")
    public Result saveOrEdit(@RequestBody OrderSetting orderSetting) {
        orderSettingService.saveOrEdit(orderSetting);
        return new Result(true, "基于日历修改预约设置成功!");
    }
}
